package com.cloudwise.controller;

import com.cloudwise.entity.ReturnBean;
import com.cloudwise.entity.Tester;
import com.cloudwise.service.TesterService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class MainController extends BaseController{

    @Resource
    private TesterService testerService;

    @RequestMapping("/toQuestion")
    public String toQuestion(){
        return "question";
    }

    @RequestMapping("/toTester")
    public String toTester(){
        return "tester";
    }

    @RequestMapping("/toExam")
    public String toExam(){
        return "exam";
    }

    @RequestMapping("/toIndex")
    public String toIndex(){
        return "index";
    }

    @RequestMapping("/toResult")
    public String toResult(){
        return "result";
    }

    @RequestMapping("/toManage")
    public String toManage(){
        return "manage";
    }

    @RequestMapping("/login")
    public String login(Tester tester, HttpSession session, ModelAndView mv){

        if (testerService.login(tester)){
            session.setAttribute("tester", tester);
            if (tester.getTesterName().equals("xxx") && tester.getTesterPhone().equals("xxxxxxxxxxx")){
                return "redirect:toManage";
            }else {
                return "redirect:toExam";
            }
        }else {
            if (testerService.phoneExist(tester.getTesterPhone())){
                mv.addObject("error", "手机号已存在");
                return "redirect:toIndex";
            }else {
                boolean save = this.testerService.save(tester);
                if (save) {
                    session.setAttribute("tester", tester);
                    return "redirect:toExam";
                } else {
                    mv.addObject("error", "用户插入失败");
                    return "redirect:toIndex";
                }
            }
        }
    }

    @PostMapping("/preLogin")
    @ResponseBody
    public ReturnBean preLogin(@RequestBody Tester tester, HttpSession session){
        if (testerService.login(tester)){
            if (tester.getTestTime() == null){
                return super.success(true);
            }else{
                session.setAttribute("tester", tester);
                return super.success(false, new Long(-1));
            }
        }else {
            if (testerService.phoneExist(tester.getTesterPhone())){
                return super.success(false);
            }else {
                boolean save = this.testerService.save(tester);
                if (save) {
                    return super.success(true);
                } else {
                    return super.success(false);
                }
            }
        }
    }
}
